package com.nitty.cash.loop.ui.activity

import android.graphics.Color
import android.view.View
import com.nitty.cash.loop.R
import com.nitty.cash.loop.common.CommonActivity
import com.nitty.cash.loop.databinding.UiProcessBinding
import com.nitty.cash.loop.databinding.UiProcessTitleItemBinding
import com.nitty.cash.loop.event.NittyEventData
import com.nitty.cash.loop.event.NittyEventPointType
import com.nitty.cash.loop.ui.contract.IViewContract
import com.nitty.cash.loop.ui.fragment.ProcessContactFragment
import com.nitty.cash.loop.ui.fragment.ProcessGeneralFragment
import com.nitty.cash.loop.ui.presenter.ProcessPresenter
import com.nitty.cash.loop.utils.NittyPickContactUtils
import com.nitty.cash.loop.utils.helper.TestDataLibInvoke
import org.json.JSONObject

/**
 * NittyCash
 * @Author wwdeng
 * @CreateTime: 2024/11/18 18:51
 * @Annotation：进件流程
 */
open class ProcessAct: CommonActivity(), IViewContract.IProcessView {
    lateinit var binding: UiProcessBinding

    var presenter: ProcessPresenter? = null
    //选择联系人
    var pickContact: NittyPickContactUtils? = null
    override fun getNittyRootView(): View? {
        binding = UiProcessBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initNittyView() {
        rootViewBinding.rootLayout.setBackgroundColor(Color.parseColor("#FFF5F5F5"))
        setNittyTitleBarVisibility(View.VISIBLE)
        setNittyBackBtnRes(R.mipmap.img_back_black)
        setNittyRightBtnRes(R.mipmap.img_customer_black)
        setNittyTextTitle(getString(R.string.steps_title))

        pickContact = NittyPickContactUtils(this)
    }

    override fun initNittyListener() {
        rootViewBinding.rootCustomer.setOnClickListener {
            val eventTitle = presenter!!.getProcessEventTitle()
            showCustomerDialog(eventTitle)
            addEventPointRequestAction(NittyEventPointType.CLICK.NITTY_CLICK_CUSTOMER,eventTitle)
        }

        //流程认证完成
        binding.btnProcessSubmit.setOnClickListener {
            addEventPointRequestAction(NittyEventPointType.CLICK.NITTY_CLICK_STEP_COMPLETE_CONTINUER)
            onIntent(LimitReviewingAct::class.java)
            finish()
        }
    }

    override fun loadNittyData() {
        if (presenter == null){
            presenter = ProcessPresenter(this)
        }
        presenter!!.loadProcessPageAction()
        //上报 Battery 和 Wifi 信息
        TestDataLibInvoke.sycPhoneStateBatteryWifiData()
    }

    /**
     * 所有步骤已完成
     */
    override fun showProcessCompleteViews() {
//        setNittyBackBtnVisibility(View.GONE)
        binding.btnProcessSubmit.visibility = View.VISIBLE
    }

    /**
     * 移除所有步骤
     */
    override fun removeProcessAllViews() {
        binding.processAfterLayout.removeAllViews()
        binding.processAfterLayout.visibility = View.GONE
        binding.processCurrentLayout.removeAllViews()
        binding.processBeforeLayout.removeAllViews()
        binding.processBeforeLayout.visibility = View.GONE
        binding.btnProcessSubmit.visibility = View.INVISIBLE
    }

    /**
     * 已完成的步骤
     */
    override fun showProcessAfterView(stepTypeDrawable: Int,stepTitle: String) {
        val itemView = UiProcessTitleItemBinding.inflate(layoutInflater)
        itemView.imgProcessType.setImageResource(stepTypeDrawable)
        itemView.textProcessTitle.text = stepTitle
        itemView.imgProcessStatus.setImageResource(R.mipmap.nitty_process_title_after_icon)

        binding.processAfterLayout.addView(itemView.root)
        binding.processAfterLayout.visibility = View.VISIBLE
    }

    /**
     * 未开始的步骤
     */
    override fun showProcessBeforeView(stepTypeDrawable: Int,stepTitle: String) {
        val itemView = UiProcessTitleItemBinding.inflate(layoutInflater)
        itemView.imgProcessType.setImageResource(stepTypeDrawable)
        itemView.textProcessTitle.text = stepTitle
        itemView.imgProcessStatus.setImageResource(R.mipmap.nitty_process_title_before_icon)

        binding.processBeforeLayout.addView(itemView.root)
        binding.processBeforeLayout.visibility = View.VISIBLE
    }

    /**
     * 联系人
     */
    override fun showProcessContactView(stepData: JSONObject) {
        replaceContainerFragement(R.id.processCurrentLayout, ProcessContactFragment(this,stepData))
    }

    /**
     * 通用样式
     */
    override fun showProcessCommonView(stepData: JSONObject) {
        replaceContainerFragement(R.id.processCurrentLayout, ProcessGeneralFragment(stepData))
    }

    /**
     * 提交
     */
    open fun onProcessSubmitAction(params: JSONObject){
        presenter!!.submitProcessAction(params)
    }

    /**
     * 流程提交成功
     * 需要发送通知刷新总流程
     */
    override fun showProcessSubmitSuccess() {
        //通知刷新总流程
        publishEventData(NittyEventData(NittyEventData.TYPE.NITTY_PROCESS_REFRESH))
    }

    override fun onNittyPublishEvent(event: NittyEventData) {
        super.onNittyPublishEvent(event)
        when(event.eventType){
            NittyEventData.TYPE.NITTY_PROCESS_REFRESHED -> {
                presenter?.loadProcessPageAction()
            }
            else -> {

            }
        }
    }

    override fun onStop() {
        super.onStop()
        binding.root.clearFocus()
    }
}